<h2>Images</h2>

<?php 
	$arg_img = "nr";
	
	if (isset($_GET[$arg_img])) {
    $id = $_GET[$arg_img];
	}
	else {
		$id = 0;
	}
	
	# page properties
	$imagesPerPage = 10;
	$offset = $id;

	$conn = connectDB();
	
	# query DB
	$q = queryDB("SELECT id from image", $conn);
	$totalNrImages =(int)mysql_numrows($q);
	
	# find total number of images in DB
	$result = queryDB("SELECT id, filename, date_time, category, country FROM image ORDER BY 'id' LIMIT $offset, $imagesPerPage", $conn);
	
	# close DB connection
	closeDB($conn);
	
	# page navigation
	$index = 1;
	$added = $index + $imagesPerPage;
	
	$counter = 0;
	echo '<center>';
	while (($index + $imagesPerPage) < ($totalNrImages + $imagesPerPage)) {
		$url = $index - 1;
		if($index >= $id && $index <= ($id+$imagesPerPage - 1)) {
			echo '<strong>['.$index.'-'.($index+$imagesPerPage - 1).']</strong>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		else {
			echo '<a href="index.php?show=images&nr='.$url.'">['.$index.'-'.($index+$imagesPerPage - 1).']</a>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		$index = $index + $imagesPerPage;
		$counter++;
		
		if($counter > 9) {
			echo '&nbsp;&nbsp;<br>';
		}
	}
	echo '<br><br>';
	
	# previous / next navigation 
	if($id == 0) {
	$next = $id + $imagesPerPage;
		echo '<- Previous&nbsp;&nbsp;&nbsp;&nbsp;';
		if($totalNrImages <= $imagesPerPage) {
			echo 'Next ->&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		else {
			echo '<a href="index.php?show=images&nr='.$next.'">Next -></a>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
	}
	# middle
	else if($id >= (0 + $imagesPerPage) && ($id + $imagesPerPage <= ($totalNrImages))) {
		echo '<a href="index.php?show=images&nr='.($id - $imagesPerPage).'"><- Previous</a>&nbsp;&nbsp;&nbsp;&nbsp;';
		echo '<a href="index.php?show=images&nr='.($id + $imagesPerPage).'">Next -></a>&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	# end
	else if(($id + $imagesPerPage) > $totalNrImages) {
		echo '<a href="index.php?show=images&nr='.($id - $imagesPerPage).'"><- Previous</a>&nbsp;&nbsp;&nbsp;&nbsp;';
		echo 'Next ->&nbsp;&nbsp;&nbsp;&nbsp;';
	}	
	echo '</center>';
	echo '<br><br>';
	
	# Display records from the table
	echo "<table border='0' cellspacing='10' cellpadding='2'>";
	while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
		echo    "<tr>
							<td rowspan='5' bgcolor='#333333' align='center'><a href=\"index.php?show=pic&id=$row[0]\" target=\"_self\"><img src='thumbnailer.php?src=$row[1]&x=250&y=187&f=0'/></a></td>
							<td>ID: $row[0]</td>
						</tr>
						<tr>
							<td>Filename: $row[1]</td>
						</tr>
						<tr>
							<td>Date time: $row[2]</td>
						</tr>
						<tr>
							<td>Category: $row[3]</td>
						</tr>
						<tr>
							<td>
							";
							if($row[4] != NULL) {
								echo "<img src='images/globe.gif' alt='This image has GPS coordinates'>";
							}
							
							echo "</td>
						</tr>
						<tr>
							<td colspan='2'><hr></td>
						</tr>";
	}
	echo "</table>";

	# previous / next navigation 
	echo '<center>';
	if($id == 0) {
	$next = $id + $imagesPerPage;
		echo '<- Previous&nbsp;&nbsp;&nbsp;&nbsp;';
		if($totalNrImages <= $imagesPerPage) {
			echo 'Next ->&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		else {
			echo '<a href="index.php?show=images&nr='.$next.'">Next -></a>&nbsp;&nbsp;&nbsp;&nbsp;';
		}	}
	# middle
	else if($id >= (0 + $imagesPerPage) && ($id + $imagesPerPage <= ($totalNrImages))) {
		echo '<a href="index.php?show=images&nr='.($id - $imagesPerPage).'"><- Previous</a>&nbsp;&nbsp;&nbsp;&nbsp;';
		echo '<a href="index.php?show=images&nr='.($id + $imagesPerPage).'">Next -></a>&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	# end
	else if(($id + $imagesPerPage) > $totalNrImages) {
		echo '<a href="index.php?show=images&nr='.($id - $imagesPerPage).'"><- Previous</a>&nbsp;&nbsp;&nbsp;&nbsp;';
		echo 'Next ->&nbsp;&nbsp;&nbsp;&nbsp;';
	}	
	echo '<br><br>';

	
	# page navigation
	$index = 1;
	$added = $index + $imagesPerPage;
	
	$counter = 0;
	echo '&nbsp;&nbsp;';
	while (($index + $imagesPerPage) < ($totalNrImages + $imagesPerPage)) {
		$url = $index - 1;
		if($index >= $id && $index <= ($id+$imagesPerPage - 1)) {
			echo '<strong>['.$index.'-'.($index+$imagesPerPage - 1).']</strong>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		else {
			echo '<a href="index.php?show=images&nr='.$url.'">['.$index.'-'.($index+$imagesPerPage - 1).']</a>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		$index = $index + $imagesPerPage;
		$counter++;
		
		if($counter > 9) {
			echo '&nbsp;&nbsp;<br>';
		}
	}
	echo '<br><br>';
	echo '</center>';	
?>